//
//  HXGroupJobTableViewController.m
//  KD
//
//  Created by ifly on 16/7/8.
//  Copyright © 2016年 Whx. All rights reserved.
// 

#import "HXGroupJobTableViewController.h"
#import "HXAddJobRecViewController.h"

#import "HXIflyJobRecordTableViewCell.h"//单元格
#import "HXIflyJobRecordDetailsTableViewController.h"
#import "NSString+Frame.h"

@interface HXGroupJobTableViewController ()

@property (nonatomic,copy) NSString * groupsId;
@property (nonatomic,strong) NSMutableArray <HXGetMembersDayWorkListModel *> * dataArr;
@property (nonatomic,assign) NSInteger num;

@end

@implementation HXGroupJobTableViewController

-(void)setIsInterFace:(NSString *)isInterFace{

    _isInterFace = isInterFace;
}

#pragma mark ----// 懒加载 \\----
-(NSMutableArray<HXGetMembersDayWorkListModel *> *)dataArr{
    
    if (_dataArr == nil) {
        
        _dataArr = [[NSMutableArray alloc] init];
    }
    
    return _dataArr;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    //加载数据
//    [self loadCreateData];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_HaveComeInGJTVC" object:nil userInfo:nil];
}
-(void)viewDidDisappear:(BOOL)animated{

    [super viewDidDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_HaveGoOutGJTVC" object:nil userInfo:nil];

}

/**
 加载数据
 */
-(void)loadCreateData{
    
    self.num = 0;
    [SVProgressHUD showWithStatus:@"正在加载..."];
    [NetworkAPI getDayWorkListWithGroupId:self.groupsId
                            withMembersId:@""
                             withPageSize:@"10"
                              withPageNum:[NSString stringWithFormat:@"%ld",(long)self.num]
                                  Success:^(HXGetMembersDayWorkModel *result) {
                                  if ([result.code isEqualToString:@"200"]) {
                                      [self.tableView.mj_footer resetNoMoreData];
                                      [SVProgressHUD showSuccessWithStatus:@"刷新成功"];
                                      [self.tableView.mj_header endRefreshing];
                                      self.num ++;
                                      [self.dataArr removeAllObjects];
                                      self.dataArr = [result.result mutableCopy];
                                      [self.tableView reloadData];
                                      if ([result.result count] < 10) {
                                          [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                          self.tableView.mj_footer.alpha = 1.0f;
                                          return;
                                      }
                                   }
                                  else if([result.code isEqualToString:@"400"]){
                                      [SVProgressHUD showErrorWithStatus:@"暂无数据"];
                                      [self.tableView.mj_header endRefreshing];
                                      [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                      self.tableView.mj_footer.alpha = 1.0f;
                                  }else{
                                      [SVProgressHUD showErrorWithStatus:@"暂无更多"];
                                      [self.tableView.mj_header endRefreshing];
                                  }
                                }
                                  failure:^(NSError *err) {
                                      [SVProgressHUD showErrorWithStatus:@"网络错误!"];
                                  }];
}

-(void)getMoreDate {
    [NetworkAPI getDayWorkListWithGroupId:self.groupsId
                            withMembersId:@""
                             withPageSize:@"10"
                              withPageNum:[NSString stringWithFormat:@"%ld",(long)self.num]
                                  Success:^(HXGetMembersDayWorkModel *result) {
                                      [SVProgressHUD dismiss];
                                      if ([result.code isEqualToString:@"200"]) {
                                          if ([result.result count] == 0) {
                                              [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                              self.tableView.mj_footer.alpha = 1.0f;
                                              return;
                                          }else if ([result.result count] < 10) {
                                              [self.dataArr addObjectsFromArray:result.result];
                                              [self.tableView reloadData];
                                              [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                              self.tableView.mj_footer.alpha = 1.0f;
                                          }else {
                                              self.num++;
                                              [self.dataArr addObjectsFromArray:result.result];
                                              [self.tableView reloadData];
                                              [self.tableView.mj_footer endRefreshing];
                                              self.tableView.mj_footer.alpha = 0;
                                          }

                                      }
                                      else if([result.code isEqualToString:@"400"]){
                                          [SVProgressHUD showErrorWithStatus:@"暂无数据"];
                                          [self.tableView.mj_header endRefreshing];
                                          [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                          self.tableView.mj_footer.alpha = 1.0f;
                                      }else{
                                          [SVProgressHUD showErrorWithStatus:@"暂无更多"];
                                          [self.tableView.mj_footer endRefreshing];
                                          self.tableView.mj_footer.alpha = 0;
                                      }
                                  }
                                  failure:^(NSError *err) {
                                      [SVProgressHUD showErrorWithStatus:@"网络错误!"];
                                  }];

    
}


/**
 重写init方法 1.需要 群组ID
 */
-(instancetype)initWithGroupId:(NSString *)groupId{

    if ([super init]) {
        
        self.groupsId = groupId;
//        [self loadCreateNotification];
    }
    
    return self;
}

///**
// 注册通知 添加监听者
// */
//-(void) loadCreateNotification{
//
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealWithJPUSH:) name:@"NOTIFICATION_DealWithJPUSH" object:nil];
//}
//
///**
// 通知的处理事件
// */
//-(void)dealWithJPUSH:(NSNotification *)notification{
//
//    NSLog(@"---------notification is %@",notification);
//    
//    [self loadCreateDataMethod];
//}

/**
 销毁通知
 */
-(void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
 加载加载的方法
 */
-(void)loadCreateDataMethod{

    self.tableView.mj_header=[AWRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadCreateData)];
    self.tableView.mj_footer=[AWRefreshFooder footerWithRefreshingTarget:self refreshingAction:@selector(getMoreDate)];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    self.tableView.mj_footer.automaticallyHidden = NO;
    [self.tableView.mj_header beginRefreshing];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"工作汇报";
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(itemClick)];
    self.navigationItem.rightBarButtonItem = item;
    [self loadCreateDataMethod];

}



-(void)itemClick {
    
    HXAddJobRecViewController * AddVC = [[HXAddJobRecViewController alloc] initWithGroupId:self.groupsId];
    
    AddVC.type = AddJobRecordType;
    AddVC.addSuperVC = self;
    
    [self.navigationController pushViewController:AddVC animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HXIflyJobRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXIflyJobRecordTableViewCell"];
    
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HXIflyJobRecordTableViewCell" owner:nil options:nil] objectAtIndex:0];
    }
    cell.isLastViewDetail = NO;
    cell.groupId = self.groupsId;
    HXGetMembersDayWorkListModel * model = self.dataArr[indexPath.section];
    cell.dayWorkModel = model;
    
    return cell;
}

/**
 返回单元格之间的间距
 */
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 4.f;
}

/**
 返回单元格的高度
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    CGFloat height = 138.f-17.f;
    
    HXGetMembersDayWorkListModel * model = self.dataArr[indexPath.section];

    if (model.Content.length > 0) {
        
        height = height + [model.Content heightWithFont:[UIFont systemFontOfSize:16] withinWidth:HXSCREEN_WIDTH-3*8];
    }
    
    
    return height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
    HXGetMembersDayWorkListModel * model = self.dataArr[indexPath.section];
    
    HXIflyJobRecordDetailsTableViewController * jobVc = [[HXIflyJobRecordDetailsTableViewController alloc] initWithMessageID:model.MessageID withSendUserID:model.SendUserID];
    
    jobVc.groupSupVC = self;
    jobVc.jiemianID = 1;
    
    [self.navigationController pushViewController:jobVc animated:YES];
}


@end
